﻿using System;
using System.Diagnostics;
using System.IO;
using System.Management;
using System.Net;
using System.Security.Principal;
using System.Windows.Forms;

namespace Easy2Java
{
    public partial class Main : Form
    {
        private int language_ZH_CN = 0;
        Language lang = new Language();
        public Main()
        {
            InitializeComponent();
            if (!IsAdministrator())
            {
                MessageBox.Show("需要管理员权限运行", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Environment.Exit(0);
            }
            LoadData();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            language_ZH_CN = 1 - language_ZH_CN;
            if (language_ZH_CN == 0)
            {
                this.button1.Text = "△中文/English";
            }
            else
            {
                this.button1.Text = "中文/△English";
            }
            this.label1.Text = lang.ll1text[language_ZH_CN];
            this.label2.Text = lang.ll2text[language_ZH_CN];
            this.label3.Text = lang.ll3text[language_ZH_CN];
            this.button2.Text = lang.bt2text[language_ZH_CN];
            this.button3.Text = lang.bt3text[language_ZH_CN];
        }
        private void LoadData()
        {
            string ll4 = "";
            string ll5 = "";
            string ll6 = "";
            string strQuery = "select *from win32_OperatingSystem";
            SelectQuery queryOS = new SelectQuery(strQuery);
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(queryOS);
            foreach (var os in searcher.Get())
            {
                ll6 = os["version"].ToString();
                ll4 = os["Caption"].ToString();
            }

            label4.Text = ll4;
            label5.Text = Environment.Is64BitOperatingSystem ? "64" : "32";
            label6.Text = ll6;
        }
        public bool IsAdministrator()
        {
            WindowsIdentity current = WindowsIdentity.GetCurrent();
            WindowsPrincipal windowsPrincipal = new WindowsPrincipal(current);
            return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                //Process p = Process.Start(Properties.Resources.setx);
                var str = Properties.Resources.setx;
                System.IO.File.WriteAllText("tmp.bat", str);
                Process p = Process.Start("tmp.bat");
                p.WaitForExit();
                System.IO.File.Delete("tmp.bat");
            }
            catch (Exception _e)
            {
                MessageBox.Show(_e.ToString());
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            bool flag = false;  //打开上次下载的文件
            long SPosition = 0; //实例化流对象
            string strFileName = "Jdk8.exe";
            FileStream FStream;
            //判断要下载的文件夹是否存在
            if (File.Exists(strFileName))
            {
                //打开要下载的文件
                FStream = File.OpenWrite(strFileName);
                //获取已经下载的长度
                SPosition = FStream.Length;
                FStream.Seek(SPosition, SeekOrigin.Current);
            }
            else
            {
                FStream = new FileStream(strFileName, FileMode.Create);
                SPosition = 0;
            }
            try
            {
                //打开网络连接
                HttpWebRequest myRequest = null;
                if (Environment.Is64BitOperatingSystem)
                {
                    myRequest = (HttpWebRequest)HttpWebRequest.Create("https://dl.softmgr.qq.com/original/Development/jdk-8u191-windows-x64-8.0.1910.12.exe");
                }
                else
                {
                    myRequest = (HttpWebRequest)HttpWebRequest.Create("https://dl.softmgr.qq.com/original/Development/jdk-8u191-windows-i586-8.0.1910.12.exe");
                }
                if (SPosition > 0)
                    myRequest.AddRange((int)SPosition);             //设置Range值
                //向服务器请求,获得服务器的回应数据流
                Stream myStream = myRequest.GetResponse().GetResponseStream();  //定义一个字节数据
                byte[] btContent = new byte[512];
                int intSize = 0;
                intSize = myStream.Read(btContent, 0, 512);
                while (intSize > 0)
                {
                    FStream.Write(btContent, 0, intSize);
                    intSize = myStream.Read(btContent, 0, 512);
                    this.Text = "Easy2Java " + "Downloading";
                }
                //关闭流
                FStream.Close();
                myStream.Close();
                flag = true;
            }
            catch (Exception)
            {
                FStream.Close();
                flag = false;
            }
            if (!flag)
            {
                if (!File.Exists(strFileName))
                {
                    MessageBox.Show("下载失败");
                }
            }
            else
            {
                Process p = Process.Start(strFileName);
                p.WaitForExit();
            }
            this.Text = "Easy2Java";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Help help = new Help(language_ZH_CN);
            help.ShowDialog();
        }
    }
    public class Language
    {
        public string[] ll1text = { "操作系统:", "OS:" };
        public string[] ll2text = { "位数:", "Bit:" };
        public string[] ll3text = { "构建代码:", "Build:" };
        public string[] bt2text = { "配置环境变量", "Set PATH" };
        public string[] bt3text = { "安装JDK", "Install JDK" };
    }
}
